from celery.utils.collections import OrderedDict
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'token': token,
        'id': user.id,
        'username': user.username
    }


class PageNum(PageNumberPagination):
    page_size = 5  # 默认每页返回的条数
    page_size_query_param = 'pagesize'  # url中设置 page_size的键,默认为page_size
    max_page_size = 10  # 每页返回的最大条数

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            ('lists', data),
            ("page", self.page.number),
            ("pages", self.page.paginator.num_pages),
            ("pagesize", self.page_size),
        ]))
